#include "Diary.h"
#include <limits>

time askTime();
day_t askDay();


time askTime(){
	int hour, minutes;
	while(true){
		cout << "Enter an hour (0-23): ";
		cin.width(2);
		cin >> hour;
		if(hour < 0 || hour > 23){
			cout << endl << "Please enter a valid hour" << endl;
			continue;
		}
		cout << endl <<  "Enter minutes (0-59): ";
		cin.width(2);
		cin >> minutes;
		if(hour < 0 || hour > 59){
			cout << endl << "Please enter valid minutes" << endl;
			continue;
		}
		cout << endl;
		return time(hour,minutes);
	}
}

day_t askDay(){
	unsigned char c;
	int i;
	while(true){
		cout << "Enter day (1-7): ";
		cin.width(1);
		cin >> c;
		if(c < '1' || c > '7'){
			cout << endl << "Please enter a valid day" << endl;
			continue;
		}
		i  = c - '0';
		i--;
		cout << endl;
		return (day_t)i;
	}
}

int main(int argc,char **argv){
	Diary d;
	cout << "Welcome to your appointment diary!" << endl;
	bool cont = true;
	unsigned char c;
	while(cont){
		cout	
				<< "	0: Make an appointment"								<< endl
				<< "	1: Remove an appointment"							<< endl
				<< "	2: Find an appointment"								<< endl
				<< "	3: Move an appointment"								<< endl
				<< "	4: Copy an appointment"								<< endl
				<< "	5: First appointment in day"						<< endl
				<< "	6: Print"											<< endl
				<< "	7: Exit"											<< endl
				<< "Enter your choice: ";
		cin.width(1);
		cin >> c;
		if(c < '0' || c > '7'){
			cout << endl << "Please enter a valid choice" << endl;
			continue;
		}
		switch(c){
			case '0':
				{
					cout << "Day" << endl;
					day_t appDay = askDay();
					cout << "Start Time" << endl;
					time appStartTime = askTime();
					cout << "End Time" << endl;
					time appEndTime = askTime();
					cout << "Enter a subject: ";
					string appSubject;
					cin.width(10);
					cin.clear();
					cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
					std::getline(cin,appSubject);
					bool bMake = d.makeAppointment(appDay,appStartTime,appEndTime,appSubject);
					if(bMake){
						cout << endl << "Appointment added!" << endl;
					}else{
						cout << endl << "Cannot add appointment, try a different time or day" << endl;
					}
				}
				break;
			case '1':
				{
					cout << "Day" << endl;
					day_t appDay = askDay();
					cout << "Start Time" << endl;
					time appStartTime = askTime();
					d.removeAppointment(appDay,appStartTime);
					cout << "Appointment was removed (if was there)" << endl;
				}
				break;
			case '2':
				{
					cout << "Day" << endl;
					day_t appDay = askDay();
					cout << "Start Time" << endl;
					time appStartTime = askTime();
					Appointment *app = d.findAppointment(appDay,appStartTime);
					if(app != 0){
						cout << *app << " was found" << endl;
					}else{
						cout << "Could not find an appointment at the given time and day" << endl;
					}
				}
				break;
			case '3':
				{
					cout << "From Day" << endl;
					day_t appFromDay = askDay();
					cout << "From Start Time" << endl;
					time appFromStartTime = askTime();
					cout << "To Day" << endl;
					day_t appToDay = askDay();
					cout << "To Start Time" << endl;
					time appToStartTime = askTime();
					bool bMake = d.moveAppointment(appFromDay,appFromStartTime,appToDay,appToStartTime);
					if(bMake){
						cout << "Appointment was moved" << endl;
					}else{
						cout << "Could not move appointment" << endl;
					}
				}
				break;
			case '4':
				{
					cout << "From Day" << endl;
					day_t appFromDay = askDay();
					cout << "From Start Time" << endl;
					time appFromStartTime = askTime();
					cout << "To Day" << endl;
					day_t appToDay = askDay();
					cout << "To Start Time" << endl;
					time appToStartTime = askTime();
					bool bMake = d.copyAppointment(appFromDay,appFromStartTime,appToDay,appToStartTime);
					if(bMake){
						cout << "Appointment was copied" << endl;
					}else{
						cout << "Could not copy appointment" << endl;
					}
				}
				break;
			case '5':
				{
					cout << "Day" << endl;
					day_t appDay = askDay();
					Appointment *app = d.firstAppointment(appDay);
					if(app != 0){
						cout << *app << " was found";
					}else{
						cout << "No appointments made for this day" << endl;
					}
				}
				break;
			case '6':
				{
					cout << d << endl;
				}
				break;
			case '7':
				{
					cont = false;
				}
				break;
			default:
				{
					cont = false;
				}
				break;
		}
	}

	return 0;
}